using System;
using System.Web.UI;


namespace WinnerCMS.WebControls
{
	public class NumericTextBox : System.Web.UI.WebControls.TextBox
	{
		public NumericTextBox()
		{
			this._decimalPlaces = - 1;
			this._js = "<script language=\'javascript\'>function extractNumber(obj, decimalPlaces, allowNegative)" + "\n" + "{" + "\n" + "	var temp = obj.value;" + "\n" + "	" + "\n" + "	// avoid changing things if already formatted correctly" + "\n" + "	var reg0Str = \'[0-9]*\';" + "\n" + "	if (decimalPlaces > 0) {" + "\n" + "       reg0Str += \'\\\\.?[0-9]{0,\' + decimalPlaces + \'}\';" + "\n" + "   } else if (decimalPlaces < 0) {"
			+ "\n" + "       reg0Str += \'\\\\.?[0-9]*\';" + "\n" + "   }" + "\n" + "	reg0Str = allowNegative ? \'^-?\' + reg0Str : \'^\' + reg0Str;" + "\n" + "	reg0Str = reg0Str + \'$\';" + "\n" + "	var reg0 = new RegExp(reg0Str);" + "\n" + "	if (reg0.test(temp)) return true;" + "\n" + "" + "\n" + "	// first replace all non numbers" + "\n" + "	var reg1Str = \'[^0-9\' + (decimalPlaces != 0 ? \'.\' : \'\') " + "+ (allowNegative ? \'-\' : \'\') + \']\';" + "\n" + "	var reg1 = new RegExp(reg1Str, \'g\');" + "\n"
			+ "	temp = temp.replace(reg1, \'\');" + "\n" + "" + "\n" + "	if (allowNegative) {" + "\n" + "		// replace extra negative" + "\n" + "		var hasNegative = temp.length > 0 && temp.charAt(0) == \'-\';" + "\n" + "		var reg2 = /-/g;" + "\n" + "		temp = temp.replace(reg2, \'\');" + "\n" + "		if (hasNegative) temp = \'-\' + temp;" + "\n" + "	}" + "\n" + "	" + "\n" + "	if (decimalPlaces != 0) {" + "\n" + "		var reg3 = /\\./g;" + "\n" + "       var reg3Array = reg3.exec(temp);" + "\n" + "       if (reg3Array != null) {"
			+ "\n" + "		    // keep only first occurrence of . " + "\n" + "   	    //  and the number of places specified by decimalPlaces " + "or the entire string if decimalPlaces < 0" + "\n" + "	        var reg3Right = temp.substring(reg3Array.index " + "+ reg3Array[0].length);" + "\n" + "	        reg3Right = reg3Right.replace(reg3, \'\');" + "\n" + "	        reg3Right = decimalPlaces > 0 ? reg3Right.substring(0, decimalPlaces) : reg3Right;" + "\n" + "	        temp = temp.substring(0,reg3Array.index) + \'.\' + reg3Right;"
			+ "\n" + "       }" + "\n" + "   }" + "\n" + "	" + "\n" + "	obj.value = temp;" + "\n" + "}" + "\n" + "function blockNonNumbers(obj, e, allowDecimal, allowNegative)" + "\n" + "{" + "\n" + "	var key;" + "\n" + "	var isCtrl = false;" + "\n" + "	var keychar;" + "\n" + "	var reg;" + "\n" + "	" + "\n" + "	if(window.event) {" + "\n" + "		key = e.keyCode;" + "\n" + "		isCtrl = window.event.ctrlKey" + "\n" + "	}" + "\n" + "	else if(e.which) {" + "\n" + "		key = e.which;" + "\n" + "		isCtrl = e.ctrlKey;" +
			"\n" + "	}" + "\n" + "	" + "\n" + "	if (isNaN(key)) return true;" + "\n" + "	" + "\n" + "	keychar = String.fromCharCode(key);" + "\n" + "		" + "\n" + "	// check for backspace or delete, or if Ctrl was pressed" + "\n" + "	if (key == 8 || isCtrl)" + "\n" + "	{" + "\n" + "		return true;" + "\n" + "	}" + "\n" + "	reg = /\\d/;" + "\n" + "	var isFirstN = allowNegative ? keychar == \'-\' && obj.value.indexOf(\'-\') == -1 : false;" + "\n" + "	var isFirstD = allowDecimal ? keychar == \'.\' && obj.value.indexOf(\'.\') == -1 : false;"
			+ "\n" + "	" + "\n" + "	return isFirstN || isFirstD || reg.test(keychar);" + "\n" + "}</script>";
		}
		
		
		private int _decimalPlaces;
		private bool _allowDecimal;
		private bool _allowNegative;
		private readonly string _js;
		
		
		protected override void OnPreRender (System.EventArgs e)
		{
			base.OnPreRender(e);
			int decPlaces=0;
			if (this._allowDecimal)
			{
				decPlaces = this._decimalPlaces;
			}
            this.Page.ClientScript.RegisterClientScriptBlock(Page.GetType(), "NumericValidation", this._js);
			this.Attributes.Add("onblur", "extractNumber(this," + decPlaces + "," + this._allowNegative.ToString().ToLower() + ");");
			this.Attributes.Add("onkeyup", "extractNumber(this," + decPlaces + "," + this._allowNegative.ToString().ToLower() + ");");
			this.Attributes.Add("onkeypress", "return blockNonNumbers(this, event," + this._allowDecimal.ToString().ToLower() + "," + this._allowNegative.ToString().ToLower() + ");");
			
		}
		
		public int DecimalPlaces
		{
			get{
				return this._decimalPlaces;
			}
			set
			{
				this._decimalPlaces = value;
			}
		}
		
		public bool AllowDecimal
		{
			get{
				return this._allowDecimal;
			}
			set
			{
				this._allowDecimal = value;
			}
		}
		
		public bool AllowNegative
		{
			get{
				return this._allowNegative;
			}
			set
			{
				this._allowNegative = value;
			}
		}
	}
	
	
}
